﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Utility.AssetTools
{
	[CreateAssetMenu (fileName = "AnimationConfig", menuName = "Utility/AnimationConfig", order = 1)]
	public class AnimationConfig : ScriptableObject
	{
		public string rigName;

		public List<string> exposedTransforms = new List<string> ();

		public List<string> loopAnimList = new List<string> ();

		public List<string> pathMustIncludedPhrase = new List<string> () { "Root_Prop", "Root_Atk_Box" };

		[Utility.Inspector.Button ("Apply")]
		public int _btn;

		public void Apply ()
		{
			string folderPath = GetFolderpath ();
			//the model-skin-avatar
			string rigPath = GetRigPath (folderPath);

			//avatar
			Avatar avatar = AssetDatabase.LoadAssetAtPath<Avatar> (rigPath) as Avatar;
			Debug.Log ($"avatar exist: { avatar != null}");
			if (avatar == null)
			{
				return;
			}

			#region Model.FBX
			ModelImporter importer_rig = (ModelImporter)AssetImporter.GetAtPath (rigPath);
			if (importer_rig == null)
			{
				Debug.Log ("Cannot find rig: " + rigPath);
				return;
			}

			foreach (string path in importer_rig.transformPaths)
			{
				foreach (var mustExposePath in pathMustIncludedPhrase)
				{
					if (path.Contains (mustExposePath) || path.Contains (mustExposePath) || path == "Root")
					{
						if (!exposedTransforms.Contains (path))
						{
							exposedTransforms.Add (path);
						}
						Debug.Log (path);
					}
				}
			}

			//model Setting
			importer_rig.preserveHierarchy = true;
			//rig Setting
			importer_rig.animationType = ModelImporterAnimationType.Generic;
			//importer_rig.sourceAvatar = avatar;
			//importer_rig.motionNodeName = rigName + "/Root";
			importer_rig.optimizeGameObjects = true;
			importer_rig.extraExposedTransformPaths = exposedTransforms.ToArray ();

			AssetDatabase.ImportAsset (rigPath);
			#endregion

			#region Animation.FBX
			List<GameObject> list_AnimImporter = AssetDatabaseEX.LoadAllAssetsAtPath<GameObject> (folderPath);

			foreach (GameObject o in list_AnimImporter)
			{
				//非对象不继续
				if (!(o is GameObject) || o.name == rigName)
				{
					continue;
				}

				//将o作为模型存储在mod中
				GameObject mod = o as GameObject;
				//将mod模型路径存储在path中
				string path = AssetDatabase.GetAssetPath (mod);
				//将路径中的模型资源导入
				ModelImporter modelimporter = AssetImporter.GetAtPath (path) as ModelImporter;
				modelimporter.preserveHierarchy = true;
				modelimporter.animationType = ModelImporterAnimationType.Generic;
				modelimporter.sourceAvatar = avatar;
				//modelimporter.SaveAndReimport ();
				AssetDatabase.ImportAsset (path);
			}

			//loop
			foreach (string name in loopAnimList)
			{
				if (string.IsNullOrEmpty (name))
				{
					continue;
				}

				string path = GetAnimPath (folderPath, name);

				ModelImporter importer = (ModelImporter)AssetImporter.GetAtPath (path);
				if (importer == null)
				{
					Debug.LogWarning ("Cannot import: " + path);
				}
				else
				{
					//animation
					ModelImporterClipAnimation[] clips = importer.defaultClipAnimations;
					foreach (ModelImporterClipAnimation clip in clips)
					{
						Debug.Log ("Set Animation Loop: " + clip.name);
						clip.loop = true;
						clip.loopTime = true;
						clip.loopPose = true;
					}
					importer.clipAnimations = clips;
					AssetDatabase.ImportAsset (path);
				}
			}
			AssetDatabase.Refresh ();
			#endregion
		}

		#region path
		string GetFolderpath ()
		{
			string assetPath = AssetDatabase.GetAssetPath (this);
			int i = assetPath.LastIndexOf ('/');
			return assetPath.Remove (i + 1);
		}

		string GetRigPath (string folderPath)
		{
			return folderPath + rigName + ".FBX";
		}

		string GetAnimPath (string folderPath, string animName)
		{
			return folderPath + rigName + "@" + animName + ".FBX";
		}
		#endregion

		void SetRootNode (ModelImporter importer, string rootName)
		{
			SerializedObject modelImporterObj = new SerializedObject (importer);
			SerializedProperty rootNodeProperty = modelImporterObj.FindProperty ("m_HumanDescription.m_RootMotionBoneName");

			rootNodeProperty.stringValue = rootName;

			modelImporterObj.ApplyModifiedProperties ();
		}
	}
}